
import Vue from 'vue'
//1.1 导入路由包
import VueRouter from 'vue-router'
import Vuex from 'vuex'
//安装路由
Vue.use(VueRouter)
Vue.use(Vuex)

var car =JSON.parse(localStorage.getItem('car')||'[]')
var store = new Vuex.Store(
    {
        state:{ //this.$store.state.###
            car:[]
        },
        mutations:{ //this.$store.commit('方法的名称'，‘按需传递唯一的参数’)
            addToCar(state,goodsinfo){
                //点击加入购物车，把商品信息保存到 store 中的 car
                var flag = false
                state.car.some(item =>{
                    if (item.id == goodsinfo.id){
                        item.count += parseInt(goodsinfo.count)
                        flag = true
                        return true //终止后续循环用return true
                    }
                    //如果最终循环完毕flag还是false，则把商品数据直接push到购物车中

                })
                if (!flag){
                    state.car.push(goodsinfo)
                }
                //当更新 car 之后，把car数组，存储到本地的 localStorage 中
                localStorage.setItem('care',JSON.stringify(state.car))
            },
            upDateGoodsInfo(state,goodsinfo){
                //修改商品数量值
                state.car.some(item=>{
                    if(item.id = goodsinfo.id){
                        item.count = parseInt(goodsinfo.count)
                        return true
                    }
                })
                localStorage.setItem('car',JSON.stringify(state.car))
            },
            removeFormCar(state,id){
                //根据id 从store 中购物车中删除那条商品的数据
                state.car.some((item,i) =>{   //(item,i)根据索引 i 删除数据
                    if(item.id == id){
                        state.car.splice(i,1)
                        return true;
                    }
                })
                localStorage.setItem('car',JSON.stringify(state.car))
            },
            updataGoodsSelected(state,info){
                state.car.some(item=>{
                    if (item.id == info.id) {
                        item.selected = info.selected
                    }
                })
                localStorage.setItem('car',JSON.stringify(state.car))
              }
        },
        getters:{
            //this.$store.getters.###
            getAllCount(state){
                var c = 0;
                state.car.forEach(item =>{
                    c += item.count
                })
                return c
            },
            getGoodsCount(state){
                var o = []
                state.car.forEach(item =>{
                    o[item.id] = item.count
                })
                return o
            },
            getGoodsSelected(state){
                var o = { }
                state.car.forEach(item =>{
                    o[item.id] = item.selected
                })
                return o
            }

        }
    }
)

import moment from 'moment'
//定义全局过滤器
Vue.filter('dateFormat',function (dataStr,pattern="") {
    return moment(dataStr).format(pattern)
})
//2.1 导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)

Vue.http.options.root='../src';
Vue.http.options.emulateJSON = true;

import 'mint-ui/lib/style.css'
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

/*import { Header, Swipe, SwipeItem, Button, Lazyload} from 'mint-ui'
Vue.component(Header.name, Header)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.component(Button.name, Button)
Vue.use(Lazyload)*/
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//安装图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//1.3 导入自己的router.js路由模块
import router from './router.js'
import app from './App.vue'

window.onload=function () {
    var vm = new Vue({
        el:'#app',
        render:c => c(app),
        router, //挂载实例对象到vm上
        store
    })
}
